<?php
// 应用公共文件

// 自定义打印函数
if(!function_exists('p')) {
    function p($data)
    {
        if(request()->isCli()) {
            print_r($data);
            echo "\r\n";
            return ;
        }
        echo '<pre>';
        if (!$data or is_bool($data)) {
            var_dump($data);
        } else {
            print_r($data);
        }
        echo '</pre>';
    }
}

// 页面搜索函数
if(!function_exists('makeSearch')) {
    function makeSearch($search, $get, $ext_button = [])
    {
        $hr_write = 0; //是否输入hr;1输入,非1,不输入
        $hr = '<span style="display:inline;margin-left: 0px;margin-right: 10px;">-</span>' . "\r\n";
        $html = '<form class="layui-form" id="_search">' . "\r\n";
        $html .= '<div class="demoTable">' . "\r\n";
        foreach ($search as $vo) {
            $html .= '<div class="layui-inline">' . "\r\n";
            switch ($vo['type']) {
                case 'select':
                    $hr_write = 0;
                    $laySearch = '';
                    if (!empty($vo['lay-search'])) {
                        $laySearch = 'lay-search';
                    }
                    $html .= '<div id="search_' . $vo['key'] . '_div" class="layui-unselect layui-form-select">' . "\r\n";
                    $html .= '<select ' . $laySearch . ' class="layui-input" id="search_' . $vo['key'] . '" name="' . $vo['key'] . '" lay-filter="filter_' . $vo['key'] . '">' . "\r\n";
                    $isIndexArr = false;
                    if(!empty($vo['data'][0]['title'])) {
                        $isIndexArr = true;
                    }
                    foreach ($vo['data'] as $lineVal => $lineKey) {
                        if($isIndexArr) {
                            $lineSelectKey = $lineKey['title'];
                            $lineSelectVal = $lineKey['id'];

                        } else {
                            $lineSelectKey = $lineKey;
                            $lineSelectVal = $lineVal;
                        }

                        if (!empty($get[$vo['key']]) and $lineSelectVal === (int)$get[$vo['key']]) {
                            $html .= '<option value="' . $lineSelectVal . '" selected>' . $lineSelectKey . '</option>' . "\r\n";
                        } else {
                            $html .= '<option value="' . $lineSelectVal . '">' . $lineSelectKey . '</option>' . "\r\n";
                        }
                    }

                    $html .= '</select>' . "\r\n";
                    $html .= '</div>' . "\r\n";
                    break;
                case 'laydate':
                    $hr_write++;
                    $html .= '<input class="layui-input" name="' . $vo['key'] . '" id="search_' . $vo['key'] . '" autocomplete="off" placeholder="' . $vo['name'] . '">' . "\r\n";
                    $html .= '<script>' . "\r\n";
                    $html .= 'layui.use(\'laydate\', function(){' . "\r\n";
                    $html .= 'var laydate = layui.laydate;' . "\r\n";
                    $html .= 'search_' . $vo['key'] . ' = laydate.render({' . "\r\n";
                    $html .= 'elem: "#search_' . $vo['key'] . '"' . "\r\n";

                    foreach ($vo['data'] as $line) {
                        $html .= ',' . $line['key'] . ':';
                        if (!empty($line['type']) and $line['type'] == 'fun') $html .= $line['value'];
                        else $html .= "'" . $line['value'] . "'";
                    }
                    $html .= '});' . "\r\n";
                    $html .= '});' . "\r\n";
                    $html .= '</script>' . "\r\n";
                    break;
                case 'input_select':
                    $hr_write = 0;

                    $html .= '<div id="search_' . $vo['key'] . '_div" class="layui-unselect layui-form-select">
                    <input type="hidden" name="' . $vo['key'] . '" id="search_' . $vo['key'] . '">
                    <select class="layui-input " id="select_' . $vo['key'] . '" lay-filter="input_select" lay-search>';
                    foreach ($vo['data'] as $line) {
                        $html .= '<option value="' . $line['id'] . '">' . $line['name'] . '</option>' . "\r\n";
                    }
                    $html .= '</select></div>';
                    $html .= '<script type="text/javascript">
                            
                        layui.use(["form"],function() {
                            var form = layui.form;
                            var $ = layui.$;
                                
                            form.on("select(input_select)", function(data) {
                                    
                                //给隐藏表单赋值
                                $("#search_' . $vo["key"] . '").val(data.value);
                            });
                        });
                        </script>';
                    break;
                default:
                    $hr_write = 0;
                    $html .= '<input class="layui-input" name="' . $vo['key'] . '" id="search_' . $vo['key'] . '" autocomplete="off" placeholder="' . $vo['name'] . '">' . "\r\n";
                    break;
            }
            $html .= '</div> &nbsp;&nbsp;';
            if ($hr_write == 1) $html .= $hr;
        }
        $html .= '<div class="layui-inline"><button type="button" class="layui-btn" data-type="reload">搜索</button></div>' . "\r\n";
        foreach ($ext_button as $v) {
            $html .= '<button class="layui-btn" data-type="' . $v['type'] . '">' . $v['title'] . '</button>' . "\r\n";
        }

        $html .= '</div>' . "\r\n";
        $html .= '</form>';
        return $html;
    }
}

// 生成随机字符串
if(!function_exists('randStr')) {
    function randStr($length=6){
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,$length);
        return $rands;
    }
}

//curl函数
if(!function_exists('curl')) {
    function curl($url, $data = false, $header = false)
    {
        if (!function_exists('curl_init')) {
            return 'curl函数库未开启！！！';
        }
        $ch = curl_init();  //初始化一个curl
        curl_setopt($ch, CURLOPT_URL, $url);     //设置接口地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   //将curl_exec以文档流的形式返回而不是直接输出

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    //关闭验证.没什么可验的.
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        if ($data) { //如果有post
            curl_setopt($ch, CURLOPT_POST, true);      //使用post方式提交
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//提交的参数 post fields
        }

        if ($header) {   //如果有header
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        $data = curl_exec($ch);    //执行curl

        if (curl_errno($ch)) {
            return false;
        }

        curl_close($ch);  //关闭curl
        return $data;
    }
}

// 字符串中间三分之一变成星号
if(!function_exists('changeToStar')) {
    function changeToStar($str) {
        if(!$str) return $str;

        $strLen = mb_strlen($str);

        // 一个字
        if($strLen === 1) {
            return $str .'**'. $str;
        }

        // 两个字
        if($strLen === 2) {
            return mb_substr($str, 0, 1) .'**'. mb_substr($str, 1);
        }

        // 三个字
        if($strLen === 3) {
            return mb_substr($str, 0, 1) .'**'. mb_substr($str, 2);
        }

        // 大于三个字
        $len1 = floor(($strLen) / 3) ?: 1;
        $star = '';
        $starCount = ceil(($strLen - $len1) / 2);
        $len2 = $len1 + $starCount;


        if($len2 >= $strLen) {
            $len2 = $strLen - 1;
        }

        for($i = 0; $i < $starCount; $i++) {
            $star .= '*';
        }
        if(!$star) $star = '*';
        $str = mb_substr($str, 0, $len1) . $star . mb_substr($str, $len2);
        return $str;
    }
}

// ip 转 整型
if(!function_exists('ip2int')) {
    function ip2int($ip)
    {
        if (!$r = ip2long($ip)) {
            return 0;
        }

        if ($r > 2147483647) {
            $r -= 4294967296;
        }

        return $r;
    }
}

if(!function_exists('int2ip')) {
    function int2ip($int) {
        return long2ip($int);
    }
}

if(!function_exists('isProduction')) {
    function isProduction() {
        return (env('ENV') == 'production');
    }
}

if(!function_exists('uniqidStr')) {
    function uniqidStr() {
        return md5(uniqid(mt_rand(), true));
    }
}
